<?php
namespace Admin\Controller;
use Think\Controller;


class GoodsController extends Controller {
	protected $model = null;

	public function __construct(){
		parent::__construct();

		$this->model= D('Home/Goods');
	}

	public function add(){
		if(!IS_POST){
                          $this->display();
		}else{
		  // echo $this->model->add($_POST)?'ok':'fail';
		 // create()会把参数, 赋给内部的data,(如果没传参,则把$_POST赋给内部data)
                     // create()的赋值过程,会检测数据的合法性, 自动验证将会发挥作用
                     //$this->model->create();
                    // echo	this->model->add() ? 'ok' : 'fail';
			if(!$this->model->create()){
				echo $this->model->getError();
			}else{
			     if(isset($_FILES['goods_img'])){
				
                                      $upload = new \Think\Upload();// 实例化上传类
                                      $upload->maxSize   =     3145728 ;// 设置附件上传大小
                                      $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                                      $upload->rootPath  =   APP_PATH; // 设置附件上传根目录
                                      $upload->savePath  =     '/Public/images/'; // 设置附件上传（子）目录
                                        // 上传文件 
                                      $info   =   $upload->upload();
                                      if(!$info) {// 上传错误提示错误信息
                                       $this->error($upload->getError());
                                       }else{// 上传至相应路径
                                        $this->model->ori_img = $info['goods_img']['savepath'] . $info['goods_img']['savename'];
                                        $this->model->goods_img = $this->model->ori_img . '230X230.jpg';
                                        $this->model->thumb_img = $this->model->ori_img . '100X100.jpg';
                                     
                                    //缩略
                                     $image = new \Think\Image(); 
                                     $image->open(APP_PATH . $this->model->ori_img);
                                      // 生成一个缩放后填充大小230*230的缩略图并保存为thumb.jpg
                                     $image->thumb(230, 230,\Think\Image::IMAGE_THUMB_FILLED);
                                     $image->save(APP_PATH . $this->model->goods_img);


                                     // 生成一个缩放后填充大小100*100的缩略图并保存为thumb.jpg
                                    $image->thumb(100, 100,\Think\Image::IMAGE_THUMB_FILLED);
                                    $image->save(APP_PATH . $this->model->thumb_img);










                                      }







                                      }

				echo $this->model->add() ? 'ok':'fail';
			       
		         }
		}
	}


	public function lists(){
		//$this->model->field('goods_id,goods_name,goods_sn,shop_price,is_best,is_new,is_hot');
		//$this->model->where('is_on_sale=1')->order('goods_id desc')->limit(0,10);
	         // $goods = $this->model->select();
	           $cat = D('Home/Cat');
	           $cats = $cat->getTree();
                $goods = D('Home/Goods');

                $_GET['p'] = $_GET['p']?$_GET['p']:1;

                  // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
                 $list = $goods->where('is_on_sale=1')->order('goods_id desc')->page($_GET['p'].',5')->select();
                 $this->assign('list',$list);// 赋值数据集
                 $count      = $goods->where('is_on_sale=1')->count();// 查询满足要求的总记录数
                 $Page       = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数
                 $show       = $Page->show();// 分页显示输出
                 $this->assign('page',$show);// 赋值分页输出
                 

                  $this->assign('cats',$cats);
	        //$this->assign('goods',$goods);
	        $this->display();

           } 


          
    public function del() {
       // $this->model->delete(I('get.goods_id'));
      // $User->where('id=5')->save($data);
       $goods_id=I('get.goods_id');
       $data['is_on_sale'] = '0';
       $goods=$this->model->where('goods_id='.$goods_id)->save($data);
      
       if($goods){
        $this->success('OK');
       }else{
        $this->error('fail');
       }
        //$this->redirect('Admin/Goods/lists');
    }



}